<?php

namespace backend\modules\models\content;

use Yii;

/**
 * This is the model class for table "content".
 *
 * @property int $id
 * @property string $title
 * @property string $content
 * @property string $image
 * @property string $is_available
 * @property int $menu_id
 * @property int $user_id
 * @property string $create_time
 */
class Content extends \common\models\Base
{

    public function behaviors()
    {
        return [
            [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['create_time'],
                ],
                'value' => date('Y-m-d H:i:s',time()),
            ]
        ];
    }


    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'content';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title','content'], 'required'],
            [['content', 'is_available'], 'string'],
            [['menu_id', 'user_id', 'times','top'], 'integer'],
            [['create_time'], 'safe'],
            [['title', 'resume', 'image'], 'string', 'max' => 255],
            [['from', 'auth'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'resume' => 'Resume',
            'content' => 'Content',
            'image' => 'Image',
            'is_available' => 'Is Available',
            'menu_id' => 'Menu ID',
            'user_id' => 'User ID',
            'from' => 'From',
            'auth' => 'Auth',
            'top' => 'Top',
            'times' => 'Times',
            'create_time' => 'Create Time',
        ];
    }


    /**
     * 添加用户id 如果用户是管理员用户 文章直接已审核
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                $this->user_id = $this->user ? $this->user->id:'';
                if($this->user_id && $this->user_id ==1){
                    $this->is_available = 1;
                }
            }
            return true;
        }
        return false;
    }


    public  function searchModel(&$page)
    {
        $find                = self::find()->filterWhere([
            'like','title',Yii::$app->request->post('title')
        ])->andFilterWhere([
            'like','resume',Yii::$app->request->post('resume')
        ])->andFilterWhere([
            'is_available'=>(string)Yii::$app->request->post('is_available')
        ])->andFilterWhere([
            'menu_id' =>  Yii::$app->request->post('menu_id')
        ])->andFilterWhere([
            'content.id' =>  Yii::$app->request->post('id')
        ])->andFilterWhere(['between','create_time',Yii::$app->request->post('create_time','')?Yii::$app->request->post('create_time','').' 00:00:00':'', Yii::$app->request->post('create_time','')?Yii::$app->request->post('create_time','').' 23:59:59':''])->joinWith(['menu']);
        $params['page_size'] = Yii::$app->request->post('page_size',self::PAGE_SIZE);
        $pagination   = new \yii\data\Pagination([
            'totalCount' => $find->count(),
            'pageSize'   => $params['page_size'],
            'page'       => Yii::$app->request->post('page',0)-1
        ]);
        $page = $find->count();
        return $find->offset($pagination->offset)->limit($pagination->limit)->orderBy('id desc')->asArray()->all();
    }

    public function getMenu()
    {
        return $this->hasOne(\backend\modules\models\setting\Menu::className(),['id'=>'menu_id']);
    }

    public function pageSearch($menus_id)
    {
        $find = self::find()->filterWhere(['menu_id'=>$menus_id,'is_available'=>1]);
        $pagination   = new \yii\data\Pagination([
            'totalCount' => $find->count(),
            'pageSize'   => 10
        ]);
        return $find->offset($pagination->offset)->limit($pagination->limit)->asArray()->all();
    }
}
